package commands.supplier
{
	import cultu.core.message.JpqlParameter;
	import cultu.supplier.message.Product;
	import cultu.supplier.message.ProductCriteria;
	import cultu.supplier.message.ProductList;
	import cultu.supplier.message.ProductScenario;
	import cultu.supplier.service.ProductManager;
	
	import model.Model;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import vo.ProductVO;
	
	import wizeoo.commands.WIZRemoteCall;
	
	public class FindProductCategory extends WIZRemoteCall
	{
		/**Référence au model*/
		private var mod:Model = Model.getInstance();
		
		public function FindProductCategory(pCallback:Function=null)
		{
			super(pCallback);
		}
		
		override public function wizExecute():void
		{
			var pm:ProductManager = new ProductManager();
			var pc:ProductCriteria = new ProductCriteria();
			var ps:ProductScenario = new ProductScenario();
			
			pc.jpqlQuery = "SELECT p FROM Product p LEFT JOIN p.communities c WHERE c.hjid=:catid";
			pc.parameter = new ArrayCollection();
			var param:JpqlParameter = new JpqlParameter();
			param.alias='catid';
			param.value=mod.selectedCategory.srcCommunity.hjid;
			pc.parameter.addItem(param);
			
			ps.manageMedias = true;
			
			pm.addEventListener(ResultEvent.RESULT,result,false,0,true);
			pm.addEventListener(FaultEvent.FAULT,fault,false,0,true);
			
			pm.findProducts(null,ps,pc);
		}
		
		override protected function wizResult(data:Object):void
		{
			mod.productsCategoryColl = new ArrayCollection();
			
			if(data && data.result)
			{
				var myColl:ArrayCollection = (data.result as ProductList).products;
				for(var i:int=0;i<myColl.length;i++)
				{
					var p:Product = myColl.getItemAt(i) as Product;
					mod.productsCategoryColl.addItem(new ProductVO(p));
				}
			}
		}
		
		override protected function wizFault(info:Object):void
		{
			trace('unable to find products');	
		}
	}
}